package com.kxf.androidtestdemo.activity

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Button
import com.kxf.androidtestdemo.R

class NotificationActivity : BaseActivity() {
    private val channelName = "23"
    private val channelID = "13"
    private val channelName2 = "232"
    private val channelID2 = "132"
    private val NOTIFICATION_ID = 4453
    private val NOTIFICATION_ID2 = 44532
    private val NOTIFICATION_TAG = "4444"
    private lateinit var notificationManager: NotificationManager
    private val NOTIFICATION_REQUEST_CODE = 784
    private var NOTIFICATION_ACTION = "NOTIFICATION_ACTION"

    var btnNotif: Button? = null
    var btnNotif2: Button? = null
    var startAty: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notification)

        notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        btnNotif = findViewById(R.id.btnNotif)
        btnNotif?.setOnClickListener {
            Log.d(tag, "btnNotif?.setOnClickListener")
            val intent = Intent(applicationContext, OkHttpActivity::class.java).apply {
                action = NOTIFICATION_ACTION
                data = Uri.parse("https://blog.csdn.net/qq_15345551")
            }
            val pendingIntent = PendingIntent.getActivity(
                applicationContext,
                NOTIFICATION_REQUEST_CODE,
                intent,
                PendingIntent.FLAG_IMMUTABLE
            )
            val notificationBuilder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
                notificationManager.createNotificationChannel(channel)
                Notification.Builder(
                    applicationContext,
                    channelID
                )
            } else {
                Notification.Builder(
                    applicationContext,
                )
            }
            notificationBuilder.apply {
                setContentTitle("111")
                setContentText("222  " + System.currentTimeMillis())
                setWhen(System.currentTimeMillis())
                setSmallIcon(R.drawable.jiuwei)
                setContentIntent(pendingIntent)
            }
            val notification = notificationBuilder.build()
            notificationManager.notify(
//                NOTIFICATION_TAG,
                NOTIFICATION_ID,
                notification
            )
        }

        btnNotif2 = findViewById(R.id.btnNotif2)
        btnNotif2?.setOnClickListener {
            Log.d(tag, "btnNotif2?.setOnClickListener")
            val intent = Intent(applicationContext, OutSideViewGroupActivity::class.java).apply {
                action = NOTIFICATION_ACTION
                data = Uri.parse("https://blog.csdn.net/qq_15345551")
            }
            val pendingIntent = PendingIntent.getActivity(
                applicationContext,
                NOTIFICATION_REQUEST_CODE,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            val notificationBuilder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(channelID2, channelName2, NotificationManager.IMPORTANCE_HIGH)
                notificationManager.createNotificationChannel(channel)
                Notification.Builder(
                    applicationContext,
                    channelID2
                )
            } else {
                Notification.Builder(
                    applicationContext,
                )
            }
            notificationBuilder.apply {
                setContentTitle("111")
                setContentText("2222  " + System.currentTimeMillis())
                setWhen(System.currentTimeMillis())
                setSmallIcon(R.drawable.jiuwei)
                setContentIntent(pendingIntent)
            }
            val notification = notificationBuilder.build()
            notificationManager.notify(
//                NOTIFICATION_TAG,
                NOTIFICATION_ID2,
                notification
            )
        }

        startAty = findViewById(R.id.startAty)
        startAty?.setOnClickListener {
            Log.d(tag, "startAty?.setOnClickListener")
            val intent = Intent(applicationContext, OutSideViewGroupActivity::class.java).apply {
                action = NOTIFICATION_ACTION
                data = Uri.parse("https://blog.csdn.net/qq_15345551")
            }
            intent.putExtra("nameKxf", "tom")
            val pendingIntent = PendingIntent.getActivity(
                applicationContext,
                NOTIFICATION_REQUEST_CODE,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            val intentAty = Intent()
            val componetName = ComponentName(
                //这个是另外一个应用程序的包名
                "com.kxf.pushkeepalive",
                //这个参数是要启动的Activity
                "com.kxf.pushkeepalive.activity.AccbActivity"
            )
            try {
                intentAty.setComponent(componetName)
                intentAty.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                intentAty.putExtra("extraPendingIntent", pendingIntent)
                startActivity(intentAty)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

    }
}